<template>
  <div style="display: flex;flex-direction: column;align-items: center;justify-content: center">
    <el-card class="box-card">
      <div class="box">
        <span>用户名：</span>
        <el-input class="el-input" v-model="person.username" placeholder="输入用户名" :disabled="disabled"/>
      </div>

    </el-card>
    <el-card class="box-card">
      <div class="box">
        <span>密&nbsp;&nbsp;&nbsp;&nbsp;码：</span>
        <el-input v-model="user.password" placeholder="输入密码" show-password :disabled="disabled"/>
      </div>
    </el-card>
    <el-card class="box-card">
      <div class="box">
        <span>性&nbsp;&nbsp;&nbsp;&nbsp;别：</span>
        <div>
          <el-radio v-model="person.sex" label="男" size="small" border :disabled="disabled">男</el-radio>
          <el-radio v-model="person.sex" label="女" size="small" border :disabled="disabled">女</el-radio>
        </div>
      </div>
    </el-card>
    <el-card class="box-card">
      <div class="box">
        <span>年&nbsp;&nbsp;&nbsp;&nbsp;龄：</span>
        <el-input v-model="person.age" placeholder="输入数字" :disabled="disabled"/>
      </div>
    </el-card>
    <el-card class="box-card">
      <div class="box">
        <span>邮&nbsp;&nbsp;&nbsp;&nbsp;箱：</span>
        <el-input v-model="person.email" placeholder="输入邮箱" :disabled="disabled"/>
      </div>
    </el-card>
    <div style="margin: 20px;">
      <el-button type="primary" size="default" icon="Setting" style="margin-right: 30px" @click="ModifyInfo()">修改</el-button>
      <el-button type="primary" size="default" icon="Upload" @click="SaveInfo()">保存</el-button>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "Information",
  data() {
    return {
      person:{
        pid:null,
        uid:null,
        username:null,
        sex:null,
        age:null,
        email:null
      },
      user:{

      },
      disabled:true,
    }
  },
  created() {
    console.log("info created!")
    let p = sessionStorage.getItem("person");
    this.person = JSON.parse(p)
    let user = {}
    request.get("/api/person/password",{
      params:{
        id:this.person.uid
      }}).then(res => {
      this.user = res.data
    })
  },
  methods:{
    ModifyInfo(){
      this.disabled = !this.disabled;
    },
    SaveInfo(){
      this.disabled = true;
      this.user.username = this.person.username;
      let params1 = {
        person:this.person,
        user:this.user
      }
      request.post("/api/person/update",params1).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "修改成功"
          })
          sessionStorage.setItem("person",JSON.stringify(this.person));
          sessionStorage.setItem("user",JSON.stringify(this.user));
          location.reload();
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
      })
    }
  }
}
</script>

<style scoped>
  span{
    width: 70px;
    white-space: pre;
  }
  .el-input{
    width: 200px;
  }
  .box-card {
    width: 300px;
    height: 60px;
    padding: 0px;
    margin: 5px;
  }
  .box {
    display: flex;
    flex-direction: row;
    font-size: 16px;
  }
</style>
